@echo off
setlocal enabledelayedexpansion
chcp 65001 >nul

echo ========================================
echo FileHive Go Edition - Build Script
echo ========================================

:: 获取参数
set PLATFORM=%1
if "%PLATFORM%"=="" set PLATFORM=all

:: 智能处理常见拼写错误
if /i "%PLATFORM%"=="window" (
    echo.
    echo 检测到可能的拼写错误: "%PLATFORM%"
    echo 你可能想输入: "windows"
    echo.
    set /p choice=是否使用 "windows" 替代？[Y/n]: 
    if /i "!choice!"=="n" (
        echo 已取消构建
        pause
        exit /b 1
    )
    set PLATFORM=windows
    echo 已自动更正为: windows
    echo.
)

if /i "%PLATFORM%"=="win" (
    echo.
    echo 检测到简写: "%PLATFORM%"
    echo 自动扩展为: "windows"
    set PLATFORM=windows
    echo.
)

if /i "%PLATFORM%"=="mac" (
    echo.
    echo 检测到简写: "%PLATFORM%"
    echo 自动扩展为: "darwin"
    set PLATFORM=darwin
    echo.
)

:: 切换到backend-go目录
cd /d "%~dp0\.."

:: 检查是否在正确的目录
if not exist "main.go" (
    echo Error: main.go not found
    pause
    exit /b 1
)

:: 检查是否存在图标文件
if not exist "assets\app.ico" (
    echo Warning: assets\app.ico not found. Run icon conversion script first.
    echo You can run: cd icon && python icon_converter.py
    pause
    exit /b 1
)

echo Target Platform: %PLATFORM%
echo Build Mode: Embedded Frontend with Icon
echo Icon File: assets\app.ico
echo.

:: 步骤1: 清理并构建前端
echo Step 1: Building frontend...
if exist "dist" rmdir /s /q "dist"
echo OK: Cleaned old dist directory

cd ..\frontend
echo Building frontend...
call npm run build
if errorlevel 1 (
    echo Frontend build failed
    cd ..\backend-go
    pause
    exit /b 1
)

echo INFO: Frontend build completed, returning to backend-go...
cd ..\backend-go
echo INFO: Current directory: %CD%

:: 步骤2: 复制前端文件
echo.
echo Step 2: Copying frontend files...
if not exist "dist" mkdir "dist"
if not exist "dist\frontend" mkdir "dist\frontend"
if not exist "dist\frontend\dist" mkdir "dist\frontend\dist"

echo Copying frontend files...
xcopy "..\frontend\dist\*" "dist\frontend\dist\" /E /Y /Q
if errorlevel 1 (
    echo Failed to copy frontend files
    pause
    exit /b 1
)
echo OK: Frontend files copied

:: 步骤3: 清理旧的资源文件
echo.
echo Step 3: Cleaning old resource files...
if exist "rsrc.syso" (
    del "rsrc.syso"
    echo OK: Cleaned old rsrc.syso
)

:: 步骤4: 构建Go程序
echo.
echo Step 4: Building Go program...

if /i "%PLATFORM%"=="windows" (
    echo Building Windows platform...
    echo Generating Windows icon resource...
    call scripts\generate-icon-resource.bat
    if errorlevel 1 (
        echo Warning: Failed to generate icon resource, building without icon
    ) else (
        echo OK: Windows icon resource generated
    )
    
    set GOOS=windows
    set GOARCH=amd64
    go build -o "dist/filehive-embedded-windows-amd64.exe" .
    if errorlevel 1 goto :build_error
    echo OK: Windows amd64 build completed
    
    set GOARCH=386
    go build -o "dist/filehive-embedded-windows-386.exe" .
    if errorlevel 1 goto :build_error
    echo OK: Windows 386 build completed
    
    :: 清理Windows专用资源文件
    if exist "rsrc.syso" del "rsrc.syso"
    
) else if /i "%PLATFORM%"=="linux" (
    echo Building Linux platform...
    set GOOS=linux
    set GOARCH=amd64
    go build -o "dist/filehive-embedded-linux-amd64" .
    if errorlevel 1 goto :build_error
    echo OK: Linux amd64 build completed
    
    set GOARCH=arm64
    go build -o "dist/filehive-embedded-linux-arm64" .
    if errorlevel 1 goto :build_error
    echo OK: Linux arm64 build completed
    
) else if /i "%PLATFORM%"=="darwin" (
    echo Building macOS platform...
    set GOOS=darwin
    set GOARCH=amd64
    go build -o "dist/filehive-embedded-darwin-amd64" .
    if errorlevel 1 goto :build_error
    echo OK: macOS amd64 build completed
    
    set GOARCH=arm64
    go build -o "dist/filehive-embedded-darwin-arm64" .
    if errorlevel 1 goto :build_error
    echo OK: macOS arm64 build completed
    
) else if /i "%PLATFORM%"=="all" (
    echo Building all platforms...
    
    :: Windows with icon
    echo Building Windows platform with icon...
    echo Generating Windows icon resource...
    call scripts\generate-icon-resource.bat
    if errorlevel 1 (
        echo Warning: Failed to generate icon resource, building Windows without icon
    ) else (
        echo OK: Windows icon resource generated
    )
    
    set GOOS=windows
    set GOARCH=amd64
    go build -o "dist/filehive-embedded-windows-amd64.exe" .
    if errorlevel 1 goto :build_error
    echo OK: Windows amd64 build completed
    
    set GOARCH=386
    go build -o "dist/filehive-embedded-windows-386.exe" .
    if errorlevel 1 goto :build_error
    echo OK: Windows 386 build completed
    
    :: Clean Windows resource file to prevent cross-platform issues
    if exist "rsrc.syso" (
        del "rsrc.syso"
        echo OK: Cleaned Windows resource file for cross-platform builds
    )
    
    :: Linux without resource file
    echo Building Linux platform...
    set GOOS=linux
    set GOARCH=amd64
    go build -o "dist/filehive-embedded-linux-amd64" .
    if errorlevel 1 goto :build_error
    echo OK: Linux amd64 build completed
    
    set GOARCH=arm64
    go build -o "dist/filehive-embedded-linux-arm64" .
    if errorlevel 1 goto :build_error
    echo OK: Linux arm64 build completed
    
    :: macOS without resource file
    echo Building macOS platform...
    set GOOS=darwin
    set GOARCH=amd64
    go build -o "dist/filehive-embedded-darwin-amd64" .
    if errorlevel 1 goto :build_error
    echo OK: macOS amd64 build completed
    
    set GOARCH=arm64
    go build -o "dist/filehive-embedded-darwin-arm64" .
    if errorlevel 1 goto :build_error
    echo OK: macOS arm64 build completed
    
    echo OK: All platforms build completed
) else (
    echo.
    echo Error: Unsupported platform "%PLATFORM%"
    echo.
    echo 支持的平台:
    echo    • windows  - 构建Windows版本 (amd64 + 386)
    echo    • linux    - 构建Linux版本 (amd64 + arm64)
    echo    • darwin   - 构建macOS版本 (amd64 + arm64)
    echo    • all      - 构建所有平台 (默认)
    echo.
    echo 常见简写也支持:
    echo    • win      - 自动转换为 windows
    echo    • mac      - 自动转换为 darwin
    echo.
    echo 用法示例:
    echo    scripts\build-with-icon.bat          # 构建所有平台
    echo    scripts\build-with-icon.bat windows  # 只构建Windows
    echo    scripts\build-with-icon.bat linux    # 只构建Linux
    echo    scripts\build-with-icon.bat darwin   # 只构建macOS
    echo.
    pause
    exit /b 1
)

echo.
echo ========================================
echo SUCCESS: Build with icon completed!
echo ========================================
echo.
echo 构建文件位置: dist/
if exist "dist\filehive-embedded-*" (
    echo.
    echo 生成的文件:
    for %%f in (dist\filehive-embedded-*) do (
        echo    %%f
        if exist "%%f" (
            for %%A in ("%%f") do echo       大小: %%~zA 字节
        )
    )
)
echo.

:: 显示图标状态
if /i "%PLATFORM%"=="windows" (
    echo 图标状态: Windows版本包含自定义高质量图标
) else if /i "%PLATFORM%"=="all" (
    echo 图标状态: Windows版本包含自定义图标，Linux/macOS使用PNG图标
) else (
    echo 图标状态: 此平台使用PNG图标文件 (assets/app.png)
)
echo.
echo 使用说明:
echo - 程序在终端中正常运行并显示输出
echo - Windows版本包含自定义高质量图标嵌入
echo - 程序支持实时访问日志显示
echo - 单文件部署，内嵌前端文件
echo - 支持跨平台运行 (Windows/Linux/macOS + ARM64)
echo.
echo 提示:
echo - 如果Windows图标模糊，请运行: scripts\clear-icon-cache.bat
echo - Linux/macOS可使用: assets/filehive.desktop 集成到桌面环境
echo.
pause
exit /b 0

:build_error
echo.
echo ERROR: Build failed for %GOOS%/%GOARCH%
echo.
echo 可能的解决方案:
echo 1. 检查Go环境和依赖: go mod tidy
echo 2. 清理构建缓存: go clean -cache
echo 3. 更新Go版本到1.21+
echo 4. 检查网络连接（如需下载依赖）
echo.
pause
exit /b 1 